//
//  PublishViewController.m
//  School
//
//  Created by 欧杨 on 2020/12/13.
//

#import "PublishViewController.h"
#import "HomeViewDemo.h"
#import <Masonry.h>
#import <AFNetworking.h>

@interface PublishViewController ()

@property (nonatomic, strong) NSString *URLString;

@end

@implementation PublishViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.detailView = [[UITextView alloc] init];
        self.titleField = [[UITextField alloc] init];
        self.isPublic = [[UISwitch alloc] init];
        self.URLString = nil;
    }
    return self;
}

- (instancetype)initWithURLString:(NSString *)urlString
{
    self = [super init];
    if (self) {
        self.detailView = [[UITextView alloc] init];
        self.titleField = [[UITextField alloc] init];
        self.isPublic = [[UISwitch alloc] init];
        [_detailView setText:@"这一刻的想法..."];
        self.URLString = urlString;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // Do any additional setup after loading the view.
    UIBarButtonItem *publish = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(publichClicked)];
    [self.navigationItem setRightBarButtonItem:publish];
    
    [_titleField setPlaceholder:@"请输入标题"];
    [_titleField setFont:[UIFont systemFontOfSize:24]];
    [self.view addSubview:_titleField];
    
    [_detailView setFont:[UIFont systemFontOfSize:22]];
    [self.view addSubview:_detailView];
    
    UILabel *swithTitle = [[UILabel alloc] init];
    [swithTitle setText:@"是否公开"];
    [self.view addSubview:swithTitle];
    [_isPublic setTitle:@"是否公开"];
    //[_isPublic setOnImage:[UIImage imageNamed:@"outline_thumb_up_alt_black_24pt"]];
    [self.view addSubview:_isPublic];
    
    UIView *line1 = [[UIView alloc] init];
    [line1 setBackgroundColor:UIColor.lightGrayColor];
    UIView *line2 = [[UIView alloc] init];
    [line2 setBackgroundColor:UIColor.lightGrayColor];
    [self.view addSubview:line1];
    [self.view addSubview:line2];
    
    [_titleField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(10);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
    }];
    
    //line
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleField.mas_bottom).offset(5);
        make.height.equalTo(@1);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [_detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@250);
        make.top.equalTo(line1.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
    }];
    
    //line
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_detailView.mas_bottom).offset(5);
        make.height.equalTo(@1);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [swithTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(10);
    }];
    [_isPublic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(swithTitle);
        make.left.equalTo(swithTitle.mas_right).offset(10);
    }];
}

- (void)publichClicked
{
    if ([self.titleField.text isEqual: @""] || [self.detailView.text isEqual: @""]) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"注意"
                                       message:@"请输入标题和内容"
                                       preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    if (self.URLString == nil) {
        NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/content/text"];
        NSDictionary *postParam = @{@"title":self.titleField.text,
                                    @"detail":self.detailView.text,
                                    @"isPublic":(self.isPublic.on ? @YES : @NO)};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager POST:URLString parameters:postParam progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@", responseObject);
            if ([responseObject[@"State"] isEqual: @"success"]) {
                NSLog(@"发布成功");
                [self.navigationController popViewControllerAnimated:YES];
                [(HomeViewDemo *)(self.navigationController.viewControllers.firstObject) getSelfContentText];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Publish request failed!");
        }];
    } else {
        NSDictionary *postParam = @{@"title":self.titleField.text,
                                    @"detail":self.detailView.text,
                                    @"isPublic":(self.isPublic.on ? @YES : @NO)};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager PATCH:self.URLString parameters:postParam success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if ([responseObject[@"State"]  isEqual: @"success"]) {
                NSLog(@"修改成功");
                [self.navigationController popViewControllerAnimated:YES];
                [(HomeViewDemo *)(self.navigationController.viewControllers.firstObject) getSelfContentText];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Change content failed!");
        }];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.detailView endEditing:YES];
    [self.titleField endEditing:YES];
}

@end
